var tmArr = new Array(50);
//count 记录当前输出的是第几个数组
var count = 0;
var t1;
//当前页面显示个数
var size = 10;
//应发总数量
var shouldCount = 0;
//实发总数量
var realCount = 0;
$(document).ready(function () {
    //加载页面时调用
    getInfo();

    //设置每2分钟循环一次
    window.setInterval(refreshInfo, 120000);
    function refreshInfo() {
        //把20秒的定时器关闭
        window.clearInterval(t1);
        console.log("=============接口读取时间============"+CurentTime()); //1495302061441
        getInfo();
    }
    window.setInterval(CurentTime, 1000);
});

//根据接口获取数据并且循环输出
function getInfo() {
    $.ajax({
        url : "http://10.0.1.100/climb/erpOrder/getDisplay",
        type : "GET",
        dataType : "jsonp", // 返回的数据类型，设置为JSONP方式
        jsonpCallback: 'success', //设置回调函数名
        success: function(res) {
            tmArr = res;
            shouldCount = 0;
            realCount = 0;
            for (var i = 0;i < tmArr.length;i++){
                if (tmArr[i].quality){
                    shouldCount+=parseInt(tmArr[i].quality);
                }
                if (tmArr[i].outQuality){
                    realCount+=parseInt(tmArr[i].outQuality);
                }
            }
            //将div标签中的数据清空
            $('#message').empty();
            var str = "";
            str +="<table border='2' rules='all' borderColor='#F00' bgcolor='#000000'>" +
                "<tr style='height: 90px;font-weight: bold;font-size: 40px;color: #28c528;text-align: left;'><th colspan='7'><div id='date' style='display:inline;margin-left:20px;'>日期："+CurentTime()+"</div><div style='display:inline;margin-left:60px;'>应发总数量："+shouldCount+"kg</div><div style='display:inline;margin-left:60px;'>实发总数量："+realCount+"kg</div></tr>" +
                "<tr style='height: 85px;font-weight: bold;font-size: 33px;font-family: SimHei;color: #28c528;text-align: center;'><th width='10%'>系统编号</th><th width='10%'>车辆信息</th><th width='30%'>客户名称</th><th width='24%'>装货产品</th>" +
                "<th width='8%'>装卸平台</th><th width='9%'>应发数量（kg）</th><th width='9%'>已发数量（kg）</th></tr>";
            //将获取的数组tmArr按照每个数组20个元素拆分成多个数组，例如：20,20,20
            var result = [];
            for(var i=0;i<tmArr.length;i+=size){
                result.push(tmArr.slice(i,i+size));
            }
            //输出第一个数组
            var firstArr = result[0];
            exportInfo(firstArr,str);
            //循环执行，每隔20秒钟执行一次 20000
            t1=window.setInterval(refreshCount, 20000);
            function refreshCount() {
                console.log("-------刷新屏幕时间---------"+CurentTime());
                //将div标签中的数据清空
                $('#message').empty();
                str = '';
                str +="<table border='2' rules='all' borderColor='#F00' bgcolor='#000000'>" +
                    "<tr style='height: 90px;font-weight: bold;font-size: 40px;color: #28c528;text-align: left;'><th colspan='7'><div id='date' style='display:inline;margin-left:20px;'>日期："+CurentTime()+"</div><div style='display:inline;margin-left:60px;'>应发总数量："+shouldCount+"kg</div><div style='display:inline;margin-left:60px;'>实发总数量："+realCount+"kg</div></tr>" +
                    "<tr style='height: 85px;font-weight: bold;font-size: 33px;font-family: SimHei;color: #28c528;text-align: center;'><th width='10%'>系统编号</th><th width='10%'>车辆信息</th><th width='30%'>客户名称</th><th width='24%'>装货产品</th>" +
                    "<th width='8%'>装卸平台</th><th width='9%'>应发数量（kg）</th><th width='9%'>已发数量（kg）</th></tr>";
                if (count >= result.length-1){
                    count = 0;
                } else {
                    count++;
                }
                //输出下一个数组
                var nextArr = result[count];

                exportInfo(nextArr,str);
            }
        }
    });
}

//输出当前数组信息
function exportInfo(exportArr,str) {
    for (var i = 0;i < 10;i++){
        if (i < exportArr.length){
            var exportData = {
                'sysCode': exportArr[i].sysCode?exportArr[i].sysCode:'',
                'vehicleInfo': exportArr[i].vehicleInfo?exportArr[i].vehicleInfo:'',
                'customerName': exportArr[i].customerName?exportArr[i].customerName:'',
                'productName': exportArr[i].productName?exportArr[i].productName:'',
                'loadUnloadPlatform': exportArr[i].loadUnloadPlatform?exportArr[i].loadUnloadPlatform:'',
                'quality': exportArr[i].quality?exportArr[i].quality:'',
                'outQuality': exportArr[i].outQuality?exportArr[i].outQuality:''
            }
            str +="<tr style='height: 70px;font-size: 30px;color: #F00;text-align: center;'><th>";
            if(exportData.sysCode.length < 10){
                str +=exportData.sysCode;
            } else {
                str +="<marquee>"+exportData.sysCode+"</marquee>";
            }
            str +="</th><th>"+exportData.vehicleInfo+"</th><th><div style='width:100%;word-wrap:break-word;'>";
            if(exportData.customerName.length < 20){
                str +=exportData.customerName;
            } else {
                str +="<marquee>"+exportData.customerName+"</marquee>";
            }
            str +="</div></th><th>"

            if(exportData.productName.length < 20){
                str +=exportData.productName;
            } else {
                str +="<marquee>"+exportData.productName+"</marquee>";
            }

            str +="</th><th style='color: orange;align-content: center;'>"+exportData.loadUnloadPlatform+"</th><th>"+exportData.quality+"</th><th>"+exportData.outQuality+"</th></tr>";
        }  else if (i >= exportArr.length) {
            str +="<tr style='height: 70px;'><th></th><th></th><th></th><th></th><th></th><th></th><th></th></tr>";
        }
    }
    //遍历完成之后
    str +="</table>";
    //将表格添加到body中
    $('#message').append(str);
}

//日期
function CurentTime() {
    $('#date').empty();
    var now = new Date();

    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日

    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
    var ss = now.getSeconds();           //秒

    var clock = year + "-";

    if(month < 10)
        clock += "0";

    clock += month + "-";

    if(day < 10)
        clock += "0";

    clock += day + " ";

    if(hh < 10)
        clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm + ":";

    if (ss < 10) clock += '0';
    clock += ss;
    $('#date').append("日期："+clock);
    return(clock);
}